#!/bin/sh

# Slackware build script for swift programming language suite

# Copyright 2017 Felipe Bugno <capent@yahoo.com>
#
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=swift
VERSION=${VERSION:-4.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCVER=swift-${VERSION}-RELEASE

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

tar xvf $CWD/swift-$SRCVER.tar.gz
mv swift-$SRCVER swift
tar xvf $CWD/swift-corelibs-foundation-$SRCVER.tar.gz
mv swift-corelibs-foundation-$SRCVER swift-corelibs-foundation
tar xvf $CWD/swift-corelibs-libdispatch-$SRCVER.tar.gz
mv swift-corelibs-libdispatch-$SRCVER swift-corelibs-libdispatch
tar xvf $CWD/swift-corelibs-xctest-$SRCVER.tar.gz
mv swift-corelibs-xctest-$SRCVER swift-corelibs-xctest
tar xvf $CWD/swift-llvm-$SRCVER.tar.gz
mv swift-llvm-$SRCVER llvm
tar xvf $CWD/swift-clang-$SRCVER.tar.gz
mv swift-clang-$SRCVER clang
tar xvf $CWD/swift-lldb-$SRCVER.tar.gz
mv swift-lldb-$SRCVER lldb
tar xvf $CWD/swift-cmark-$SRCVER.tar.gz
mv swift-cmark-$SRCVER cmark
tar xvf $CWD/swift-package-manager-$SRCVER.tar.gz
mv swift-package-manager-$SRCVER swiftpm
tar xvf $CWD/swift-llbuild-$SRCVER.tar.gz
mv swift-llbuild-$SRCVER llbuild
tar xvf $CWD/swift-compiler-rt-$SRCVER.tar.gz
mv swift-compiler-rt-$SRCVER compiler-rt
tar xvf $CWD/swift-integration-tests-$SRCVER.tar.gz
mv swift-integration-tests-$SRCVER swift-integration-tests

find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Currently, we have two problems with swift on non-Ubuntu distros:
#
# 1 - need to fix for /usr/lib64
#
# 2 - Swift Package Manager on Linux has some Debian/Ubuntu specific stuff.
# The installation script does tests on the package manager using XCTest,
# and of course, it will fail in Slackware because it does not recognize the
# running platform, so they can't determine if we use one of the two providers
# hard coded (APT and Brew). We must override this rule on its XCTest source.
# All other tests should pass.
#
# As a note: it is a useless feature anyway. Swiftpm never install system
# packages by itself neither on Debian or OS X. This feature is only to
# hint to the user about needed system packages, printing in the console
# the brew or apt-get command that he needs to type in order to install a
# system package needed by a swift package.
#
patch -p1 < $CWD/swift-libdir.patch

cd swift

utils/build-script \
  -R -l -b -p \
  --xctest \
  --foundation \
  -i \
  --tvos 1 \
  --watchos 1 \
  --no-swift-stdlib-assertions \
  --build-swift-static-stdlib \
  --build-swift-static-sdk-overlay \
  --build-swift-stdlib-unittest-extra \
  --skip-test-lldb \
  --install-destdir=$PKG \
  --install-prefix=usr \
  --install-swift \
  --install-lldb \
  --install-foundation \
  --install-llbuild \
  --install-swiftpm \
  --install-xctest \
  --install-libdispatch \
  --swift-install-components='autolink-driver;compiler;clang-builtin-headers;stdlib;swift-remote-mirror;sdk-overlay;license;sourcekit-inproc' \
  --reconfigure

# Stripping debug symbols from repl_swift and its libraries would defeat its
# purpose as a debuggable interactive shell. If you strip it, when you call lldb --repl
# you will be met with a error message due to repl_swift's lack of breakpoints.
# The important part was compiled as a cmake "Release" and lacks debug symbols.

# Rename /usr/bin/lldb* to lldb-swift*
for old in $PKG/usr/bin/lldb*; do
  new=$(echo "$old" | sed 's/lldb/lldb-swift/')
  if [ -L "$old" ]; then
    newtarget=$(readlink "$old" | sed 's/lldb/lldb-swift/')
    rm "$old"
    ln -sf "$newtarget" "$new"
  else
    mv "$old" "$new"
  fi
done

# Python: fix the symlink we broke above, remove modules that clash with existing packages
for sitepython in $PKG/usr/lib$LIBDIRSUFFIX/python*/site-packages; do
  if [ -d "$sitepython"/lldb ]; then
    ln -sf ../../../../bin/lldb-swift-argdumper "$sitepython"/lldb/lldb-argdumper
  fi
  rm -f "$sitepython"/six.py
  rm -f "$sitepython"/readline.so
done

mv $PKG/usr/share/man $PKG/usr/man
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

# Remove junk directories
rm -rf $PKG/usr/local $PKG/usr/share

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CODE_OWNERS.TXT LICENSE.txt README.md CONTRIBUTING.md CHANGELOG.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
